package com.heartware.sh.blackhole.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.heartware.sh.blackhole.common.FragmentFactory;
import com.heartware.sh.blackhole.entity.FragmentInfo;
import com.heartware.sh.blackhole.ui.fragment.MyFragment;
import com.heartware.sh.blackhole.ui.fragment.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/24 0024.
 */
public class TabAdapter extends FragmentPagerAdapter {

    private String[] strList = {"推荐","热点","科技","生活","健康","西安","文章","Android开发","Android技术","哲学"};

    private List<FragmentInfo> fragmentInfos = new ArrayList<>();


    public TabAdapter(FragmentManager fm) {
        super(fm);
        init();
    }

    public void init(){

        for(int i=0; i<strList.length; i++){

           fragmentInfos.add(FragmentFactory.createFragmentInfo(i, strList[i]));

        }

    }

    @Override
    public BaseFragment getItem(int position) {

        MyFragment myFragment = null;
        try {
            myFragment = (MyFragment) fragmentInfos.get(position).getFragmentInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return myFragment;

    }

    @Override
    public int getCount() {

        return fragmentInfos.size();

    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentInfos.get(position).getTitle();
    }
}
